package com.xiaomengkuan.test.service.impl;

import com.xiaomengkuan.test.dao.CommentRepository;
import com.xiaomengkuan.test.dao.UserRepository;
import com.xiaomengkuan.test.entity.pojo.Comment;
import com.xiaomengkuan.test.entity.pojo.User;
import com.xiaomengkuan.test.entity.vo.CommentVo;
import com.xiaomengkuan.test.service.CommentService;
import com.xiaomengkuan.test.util.YsjUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Faraj Mujey
 * -- Creation Time: 2021-01-09 8:37 下午, File Encoding: utf8 --
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    UserRepository userRepository;
    @Resource
    CommentRepository commentRepository;

    @Override
    public Map<String, Object> getCommentsByProductId(int id) {
        List<Comment> comments =
                commentRepository.findAllByProductIdAndBaseId(id, 0);
        final int[] count = {comments.size()};
        List<CommentVo> list = comments.stream().map(entity -> {
            CommentVo commentVo = new CommentVo();
            commentVo.setContext(entity.getContext());
            commentVo.setId(entity.getId());
            int userId = entity.getUserId();
            // 时间字符串
            commentVo.setCreatedTme(YsjUtil.getTimeStr(entity.getCreatedTime()));
            commentVo.setTimestamp(entity.getCreatedTime().getTime());
            // 用户信息
            User user = userRepository.getOne(userId);
            commentVo.setNickname(user.getNickname());
            commentVo.setAvatar(user.getAvatar());
            // 回复的评论
            List<Comment> replayCommentObjs =
                    commentRepository.findAllByProductIdAndBaseId(id,
                            commentVo.getId());
            count[0] += replayCommentObjs.size();
            List<CommentVo> replayComments = replayCommentObjs.stream().map(r -> {
                CommentVo vo = new CommentVo();
                vo.setContext(r.getContext());
                vo.setId(r.getId());
                int replayUserId = r.getUserId();
                // 时间字符串
                vo.setCreatedTme(YsjUtil.getTimeStr(r.getCreatedTime()));
                // 用户信息
                User user1 = userRepository.getOne(replayUserId);
                vo.setTimestamp(r.getCreatedTime().getTime());
                vo.setNickname(user1.getNickname());
                vo.setAvatar(user1.getAvatar());
                return vo;
            }).sorted((o1,o2) -> (int) (o2.getTimestamp() - o1.getTimestamp())).collect(Collectors.toList());
            commentVo.setReplayComment(replayComments);
            return commentVo;
        }).sorted((o1,o2) -> (int) (o2.getTimestamp() - o1.getTimestamp())).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("count", count);
        map.put("list", list);
        return map;
    }

}
